import {
	post,
	get
} from '@/utils/request.js'
export default {
	state: {
		cartList: [],
		// 选中列表（存放选中的id）
		selectedList: [],
		cartDetail: {}
	},
	getters: {
		// 判断是否全选
		checkedAll: (state) => {
			return state.cartList.length === state.selectedList.length
		},
		// 合计
		totalPrice: (state) => {
			
			var total = 0
			state.cartList.forEach(v => {
				let i = state.selectedList.findIndex(res => {
					return res.id == v.id
				})
				if (i > -1) {
					total += v.goods.price * v.num
				}
			})
			return total.toFixed(2)
		},
		// 禁用全选
		disableSelectAll: (state) => {
			return state.cartList.length === 0
		},
		// 购物车商品数量
		cartCount: (state) => {
			if (state.cartList.length <= 99) {
				return state.cartList.length
			}
			return '99+'
		},
		confirmCount: (state) => {
			return state.selectedList.length
		}

	},
	mutations: {
		// 初始化list
		initCartList(state, list) {
			console.log(list)
			let newList = list.map(v => {
				return {
					id: v.id,
					uid: v.uid,
					goods_id: v.goods_id,
					num: v.num,
					goods: v.goods,
					isActive: false
				}
			})
			state.cartList = newList
			state.selectedList = []
			state.cartList.forEach((v) => {
				if (v.isActive) {
					state.selectedList.push(v)
				}
			})
		},
		// 初始化购物详情
		initCartDetail(state, data) {
			state.cartDetail = data
		},
		// 选中/取消选中某个商品
		selectItem(state, index) {
			let id = state.cartList[index].id
			
			let i = state.selectedList.findIndex(res => {
				return res.id == id
			})
			
			// 之前是选中，取消选中
			if (i > -1) {
				// 取消当前商品选中状态
				state.cartList[index].isActive = false
				// 移除选中列表中的当前商品
				return state.selectedList.splice(i,1)
			}
			
			// 选中
			state.cartList[index].isActive = true
			state.selectedList.push(state.cartList[index])
		},
		// 全选
		selectAll(state) {
			state.selectedList = state.cartList.map(v => {
				// 设置选中状态
				v.isActive = true
				return v
			})
		},
		// 取消全选
		unSelectAll(state) {
			state.cartList.forEach(v => {
				// 设置选中状态
				v.isActive = false
			})
			state.selectedList = []
		},
		// 删除选中
		delGoods(state) {
			state.cartList = state.cartList.filter(v => {
				return state.selectedList.indexOf(v.id) === -1
			})
			// $U.updateCartBadge(state.list.length)
		},
		delGoodsOne(state, index) {
			console.log(index)
			state.cartList.splice(index, 1)
		},
		// 加入购物车
		addGoodsToCart(state, goods) {
			state.cartList.unshift(goods)
		},
		// 清空购物车
		clearCart(state) {
			state.cartList = []
			state.selectedList = []

		}
	},
	actions: {
		// 更新购物车列表
		updateCartList({
			state,
			commit
		}) {
			get('/api/services/public/CartItem/GetAll').then(res => {
				commit('initCartList', res.result)
			})
		},
		doSelectAll({
			commit,
			getters
		}) {
			getters.checkedAll ? commit('unSelectAll') : commit('selectAll')
		},
		doDelete({
			commit,
			state
		}, {
			id,
			index
		}) {
			uni.showModal({
				content: '是否删除选中',
				success: (res) => {
					if (res.confirm) {
						post('/api/services/public/CartItem/Detlet', {
							id: id
						}).then(res => {
							commit('delGoodsOne', index)
							uni.showToast({
								title: '删除成功'
							});
						})
					}
				}
			});
		}
	}
}
